//
//  HEIMAudioCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/28.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEIMAudioCell.h"
#import "HEIMAudioObject.h"
#import "HEMediaManager.h"

@interface HEIMAudioCell ()<HEIMMediaManagerDelegate>

@property (nonatomic, strong) UILabel *audioDurationLable; //语音时长
@property (nonatomic, strong) UIImageView *voiceImageView; //

@end

@implementation HEIMAudioCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];

        [self makeGesture];
    }
    return self;
}

- (void)makeGesture {
    [super makeGesture];

    UITapGestureRecognizer *contentTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentClick)];
    [self.bubbleView addGestureRecognizer: contentTap];
}

- (void)contentClick {
    [[HEMediaManager sharedManager] addDelegate:self];

    if ([self.delegate respondsToSelector:@selector(onTapMessage:)]) {
        [self.delegate onTapMessage: self.model.message];
    }
}

- (void)initUI {
    
    _audioDurationLable = [UILabel new];
    _audioDurationLable.text = @"0''";
    _audioDurationLable.font = DYFont(16);
    [self.bubbleView addSubview:_audioDurationLable];
    
    _voiceImageView = [UIImageView new];
    _voiceImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.bubbleView addSubview:_voiceImageView];

    [_voiceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_audioDurationLable);
        make.right.equalTo(self.bubbleView).mas_offset(-10);
        make.width.mas_offset(20);
    }];
    
   [_audioDurationLable mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(self.bubbleView).mas_offset(15);
       make.left.equalTo(self.bubbleView).mas_offset(10);
       make.right.equalTo(_voiceImageView.mas_left).mas_offset(-60);
       make.bottom.equalTo(self.bubbleView).mas_offset(-15);
   }];
}

- (void)configCellWithModel:(HEIMMessageModel *)model {
    self.model = model;
    HEIMAudioObject *audio = (HEIMAudioObject *)model.message.messageObject;
    _audioDurationLable.text = [NSString stringWithFormat:@"%ld''", audio.duration];
    _audioDurationLable.textColor = model.warpTextColor;
    
    [self setupDefualtImage];

    [self setupAnimation];

    // 必须写
    [self refreshData:model];
}

/// 设置动画图片
- (void)setupAnimation {
    _voiceImageView.animationImages = self.model.audioAnimotionImages;
    _voiceImageView.animationDuration = 1.0;
    
    [self setPlaying: self.isPlaying];
}

- (void)setupDefualtImage {
    _voiceImageView.image = self.model.audioAnimotionImages[0];
}

- (void)setPlaying:(BOOL)isPlaying
{
    if (isPlaying) {
        [self.voiceImageView startAnimating];
    }else{
        [self.voiceImageView stopAnimating];
    }
}

#pragma mark - NIMMediaManagerDelegate

- (void)playAudio:(NSString *)filePath didBeganWithError:(NSError *)error {
    [self startPlayingUI]; 
}

- (void)playAudio:(NSString *)filePath didCompletedWithError:(NSError *)error
{
    [self stopPlayingUI];
}

- (void)stopPlayAudio:(NSString *)filePath didCompletedWithError:(nullable NSError *)error
{
    [self stopPlayingUI];
}

#pragma mark - private methods
- (void)startPlayingUI
{
    [self setPlaying:YES];
}

- (void)stopPlayingUI
{
    [self setPlaying:NO];
}

- (BOOL)isPlaying
{
    return [HEMediaManager sharedManager].currentPlayingMessage == self.model.message;
}

- (void)dealloc {
    [[HEMediaManager sharedManager] removeAllDelegate];
}

@end
